package com.tanhua.dubbo.server.api;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.net.CacheRequest;
import java.util.List;

@Service(version = "1.0.0")
public class VideoMassageApiImpl implements VideoMassageApi  {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageInfo<Video> queryVideoList(String userid, String sortProp, String sortOrder) {

        PageInfo<Video> videoPageInfo = new PageInfo<>();


        Query query = null;
        if (StringUtils.equalsIgnoreCase(sortOrder,"descending ")){
            query = Query.query(Criteria.where("userId").is(Convert.toLong(userid))).with(Sort.by(Sort.Order.desc("created")));
        }else {
            query = Query.query(Criteria.where("userId").is(Convert.toLong(userid))).with(Sort.by(Sort.Order.desc("created")));
        }


        List<Video> videos = this.mongoTemplate.find(query, Video.class);
        System.out.println(videos);

        videoPageInfo.setRecords(videos);

        Integer counts = getCounts(userid);

        videoPageInfo.setTotal(counts);

        return videoPageInfo;
    }

    @Override
    public Integer getCounts(String userid) {
        Query query = new Query(Criteria.where("userId").is(Convert.toLong(userid)));
        List<Video> videoList = this.mongoTemplate.find(query,Video.class);

        return videoList.size();
    }


}
